package com.zhiyou.crud.service.impl;

import com.zhiyou.crud.bean.Back;
import com.zhiyou.crud.bean.Project;
import com.zhiyou.crud.bean.Real;
import com.zhiyou.crud.bean.Type;
import com.zhiyou.crud.dao.BackMapper;
import com.zhiyou.crud.dao.ProjectMapper;
import com.zhiyou.crud.dao.RealMapper;
import com.zhiyou.crud.dao.TypeMapper;
import com.zhiyou.crud.service.ProjectPublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ProjectPublishServiceImpl implements ProjectPublishService {

    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    TypeMapper typeMapper;

    @Autowired
    RealMapper realMapper;


    @Autowired
    BackMapper backMapper;

    //得到所有项目类型
    @Override
    public List<Type> getAllTypes() {
        return typeMapper.selectByExample(null);
    }

    //根据uno判断用户是否已实名
    @Override
    public boolean getRealInfo(Integer uno) {
        Example example = new Example(Type.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uno",uno);
        List<Real> reals = realMapper.selectByExample(example);

        if(reals.size()>0){
            return true;
        }
        return false;
    }


    //根据uno得到草稿项目  每人限制一个
    @Override
    public Project getProjectTemp(Integer uno) {
        Example example = new Example(Project.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uno",uno);
        criteria.andEqualTo("pstatus",0);

        List<Project> projects = projectMapper.selectByExample(example);

        if(projects.size()>0){
            return projects.get(0);
        }
        return null;
    }


    //更新临时项目
    @Override
    public boolean updateTempProject(Project project) {

        projectMapper.updateByPrimaryKeySelective(project);
        return true;
    }

    @Override
    public List<Back> getBacksByPno(Integer pno) {
        Example example = new Example(Back.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pno",pno);
        return backMapper.selectByExample(example);
    }

    @Override
    public int publishProject(Project project) {
        project.setPstatus(1);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String publishDate = simpleDateFormat.format(date);
        project.setPsubmittime(publishDate);
        return projectMapper.updateByPrimaryKeySelective(project);
    }

    @Override
    public int deleteBack(Back back) {
        return backMapper.deleteByPrimaryKey(back);
    }

    @Override
    public int addBack(Back back) {
        return backMapper.insert(back);
    }

    @Override
    public int addProject(Project project) {
        return projectMapper.insertSelective(project);
    }


}
